package com.zz.service.impl;


import com.zz.entity.Box;
import com.zz.entity.ESBox;
import com.zz.repository.ESBoxRepository;
import com.zz.service.BoxService;
import com.zz.service.ESBoxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/***
 * @author 周紫
 **/
@Service
public class ESBoxServiceImpl implements ESBoxService {


    @Autowired
    private ESBoxRepository esBoxRepository;

    @Autowired
    private BoxService boxService;

    //es添加数据
    public void addToES(String code) {
        ESBox esBox = new ESBox();
        Box box = boxService.getById(code);
        BeanUtils.copyProperties(box,esBox);
        esBoxRepository.save(esBox);
        searchById(esBox.getId().toString());
    }

    //es修改数据
    public String esUpdate(ESBox esBox){
        // 注意，es是根据id修改信息的，id必须是es里面的id,而非mysql数据库里面的id
        ESBox box=searchById(esBox.getId().toString());
        if (box!=null){
            esBoxRepository.save(esBox);
            return "ok";
        }
        return "error";
    }
    public void esDelete(ESBox esBox){
        // 注意，es是根据id删除信息的，id必须是es里面的id,而非mysql数据库里面的id
        esBoxRepository.deleteById(esBox.getId().toString());
    }


    public ESBox searchById(String id) {
        Optional<ESBox> esBox = esBoxRepository.findById(id);
        return esBox.get();
    }


}
